import * as React from 'react';
import * as qs from 'qs';
import {defaultAuthConfig as config} from './config/oauth.config'

export interface OAuthLoginProps {

}

class OAuthLogin extends React.Component<OAuthLoginProps, any> {
  static defaultProps = {};

  componentDidMount() {
    if (this.isLogin()) {
      this.goHome();
      return;
    }
    this.authorizeLogin();
  }

  private authorizeLogin() {
    window.location.assign(`${config.authorizeUrl}?${qs.stringify({
      response_type: config.response_type,
      client_id: config.client_id,
      scope: 'get_user_info',
      redirect_uri: config.redirect_uri,
      state: 'xxxxx-xxxxx'.replace(/x/g, () => (Math.floor(Math.random() * 16)).toString(16)),
    })}`);
  }

  private goHome() {
    console.log('Home ')
  }

  private isLogin() {
    return false;
  }

  render() {
    return null;
  }
}

export default OAuthLogin;
